package com.cnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cnt.dao.PointsDao;

import com.cnt.dao.PointsRulesDao;
import com.cnt.entity.*;
import com.cnt.service.PointsService;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PointsServiceImpl implements PointsService {
    private static final String IMAGE_DIR = "src/main/resources/static/points/";
    @Autowired
    private PointsDao pointsDao;
    @Autowired
    private PointsRulesDao pointsRulesDao;

    @Override
    public ResultVo<List<Points>> getPointsList() {
        List<Points> points = pointsDao.selectList(null);
        return new ResultVo<>(0, "查询成功", points);
    }

    @Override
    public ResultVo<Void> createPoints(Points points) {
        points.setCreatedAt(Calendar.getInstance().getTime());
        int insert = pointsDao.insert(points);
        if (insert == 1) {
            return new ResultVo<>(0, "添加成功", null);
        }
        return new ResultVo<>(1, "添加失败", null);
    }

    @Override
    public ResultVo<Void> changePoints(Points points) {
        Points points1 = pointsDao.selectById(points.getPointsId());
        if (points1 == null) {
            return new ResultVo<>(1, "找不到此规则编号，修改失败", null);
        }
        int i = pointsDao.updateById(points);
        if (i == 1) {
            return new ResultVo<>(0, "修改成功", null);
        }
        return new ResultVo<>(2, "修改失败", null);
    }

    @Override
    public ResultVo<Void> deletePoints(Integer pointsId) {
        Points points = pointsDao.selectById(pointsId);
        if (points == null) {
            return new ResultVo<>(1, "找不到此积分表，删除失败", null);
        }
        if (points.getIsInUse() == 1) {
            return new ResultVo<>(2, "积分表已经使用过了，不可删除", null);
        }
        pointsDao.deleteById(pointsId);
        return new ResultVo<>(0, "删除成功", null);
    }

    @Override
    public ResultVo<List<PointsRules>> getPointsInfo(Integer pointsId) {
        QueryWrapper<PointsRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("points_id", pointsId);
        List<PointsRules> pointsRules = pointsRulesDao.selectList(queryWrapper);
        return new ResultVo<>(0, "查询成功", pointsRules);
    }

    @Transactional
    @Override
    public ResultVo<Void> addPointsRule(Integer pointsId, Integer count) {
        for (int i = 0; i < count; i++) {
            pointsRulesDao.insert(new PointsRules(0, pointsId, null, null, null));
        }

        return new ResultVo<>(0, "新建成功", null);
    }

    @Override
    public ResultVo<Void> uploadPointsRule(PointsRules pointsRules) {
        if (pointsRules.getPointsRulesId() == 0) {
            return new ResultVo<>(1, "找不到此积分规则", null);
        }
        LambdaUpdateWrapper<PointsRules> luw = new LambdaUpdateWrapper<>();
        luw.eq(PointsRules::getPointsRulesId, pointsRules.getPointsRulesId());
        int i = pointsRulesDao.update(pointsRules, luw);
        if (i == 0) {
            return new ResultVo<>(2, "修改失败", null);
        }
        return new ResultVo<>(0, "修改成功", null);
    }

    @Override
    public ResultVo<Void> deletePointsRule(Integer pointsRuleId) {
        if (pointsRuleId == 0) {
            return new ResultVo<>(1, "找不到此积分规则", null);
        }
        int i = pointsRulesDao.deleteById(pointsRuleId);
        if (i == 1) {
            return new ResultVo<>(0, "删除成功", null);
        }
        return new ResultVo<>(2, "删除失败", null);
    }

    @Transactional
    @Override
    public ResultVo<Void> replacePoints(Integer pointsId) {
        LambdaQueryWrapper<PointsRules> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PointsRules::getPointsId, pointsId);
        List<PointsRules> pointsRulesList = pointsRulesDao.selectList(lqw);
        for (PointsRules pointsRules : pointsRulesList) {
            int pointsRulesId = pointsRules.getPointsRulesId();
            String ruleTitle = pointsRules.getRuleTitle();
            String details = pointsRules.getDetails();
            Integer pointsValue = pointsRules.getPointsValue();
            if (ruleTitle == null || ruleTitle == "" || details == null || ruleTitle == "" || pointsValue == 0) {
                return new ResultVo<>(2, "ID为" + pointsRulesId + "规则没有设计完整，发布失败", null);
            }
        }
        pointsDao.updateIsInUse();
        Points points = pointsDao.selectById(pointsId);
        points.setIsInUse(1);
        points.setIsUsed(1);
        int i = pointsDao.updateById(points);
        if (i == 1) {
            return new ResultVo<>(0, "成功使用新的积分表", null);

        }
        return new ResultVo<>(1, "操作失败", null);
    }

    @Transactional
    @Override
    public ResultVo<Void> copyPoints(Integer pointsId) {
        Points points = pointsDao.selectById(pointsId);
        if (points == null) {
            return new ResultVo<>(1, "操作失败", null);
        }
        points.setTitle(points.getTitle() + "_copy");
        points.setIsUsed(0);
        points.setIsInUse(0);
        points.setCreatedAt(Calendar.getInstance().getTime());
        //查询要copy对象的积分规则
        QueryWrapper<PointsRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("points_id", pointsId);
        List<PointsRules> pointsRules = pointsRulesDao.selectList(queryWrapper);
        pointsDao.insert(points);
        int newPointsId = points.getPointsId();//获取主键回填的数值
        for (int i = 0; i < pointsRules.size(); i++) {
            PointsRules P = pointsRules.get(i);
            P.setPointsId(newPointsId);
            pointsRulesDao.insert(pointsRules.get(i));
        }
        return new ResultVo<>(0, "操作成功", null);
    }

    @Override
    public ResultVo<Void> uploadFile(MultipartFile file, Integer pointsId) {
        if (file.getSize() > 16 * 1024 * 1024) { // 限制文件大小为5MB
            return new ResultVo<>(3, "图片文件大小不能超过16MB", null);
        }
        Points points = pointsDao.selectById(pointsId);
        try {
            points.setFile(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = pointsDao.updateById(points);
        if (i == 1) {
            return new ResultVo<>(0, "上传成功", null);
        }
        return new ResultVo<>(1, "上传失败", null);
    }

    @Override
    public InputStream download(Integer pointsId) {
        byte[] file = pointsDao.selectById(pointsId).getFile();
        return new ByteArrayInputStream(file);
    }

    @Override
    public ResultVo<List<PointsRules>> getPoints(Integer pointsId) {
        QueryWrapper<PointsRules> wrapper = new QueryWrapper<>();
        wrapper.eq("points_id", pointsId);
        List<PointsRules> pointsRules = pointsRulesDao.selectList(wrapper);
        return new ResultVo<>(0, "查询成功", pointsRules);
    }

    @Override
    public ResultVo<List<PointsRules>> getByIsPointsRules(String isPointsRules) {
        List<Integer> resultList = Arrays.stream(isPointsRules.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        List<PointsRules> list = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {
            list.add(pointsRulesDao.selectById(resultList.get(i)));
        }
        return new ResultVo<>(0, "查询成功", list);
    }
}
